﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    /// <summary>
    /// 演示
    /// </summary>
    public partial class AnaControl : UserControl
    {
        AnaPainter anaPainter;
        Timer timer;

        bool isInvalid = true;
        public AnaControl()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.ResizeRedraw
                | ControlStyles.SupportsTransparentBackColor
                | ControlStyles.UserPaint,true );


            anaPainter = new AnaPainter(this, new Rectangle(0, 0, this.Width, this.Height));


            timer = new Timer();
            timer.Interval = 100;
            timer.Tick += delegate(object sender, EventArgs e)
            {
                if (isInvalid)
                {
                    this.Invalidate();
                    isInvalid = false;
                }
            };

            SimpleItem item = new SimpleItem(
                "我对不起世界上所有的女人，因为我犯了天底下所有男人都会犯的错误。",
                "",
                DateTime.Now,
                "坏男孩",
                "",
                Properties.Resources.AuthorExemple,
                "公元2008年09月05日");
            anaPainter.DisplayItem = item;

            anaPainter.Invalidated += delegate(object sender, InvalidateEventArgs e) { isInvalid = true; };

            anaPainter.OprMode = true;
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            timer.Enabled = this.Visible;
            base.OnVisibleChanged(e);
        }

        public void UpdateTheme(ThemeData theme)
        {
            anaPainter.UpdateTheme(theme);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (anaPainter != null)
            {

                if (this.Width > 0 && this.Height > 0)
                {
                    anaPainter.Bounds = new Rectangle(0, 0, this.Width, this.Height);
                }
            }

            base.OnSizeChanged(e);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: 在此处添加自定义绘制代码
            anaPainter.Paint(new PaintEventArgs(pe.Graphics, new Rectangle(0, 0, this.Width, this.Height)));

            // 调用基类 OnPaint
            base.OnPaint(pe);
        }


        protected override void OnMouseClick(MouseEventArgs e)
        {
            anaPainter.OnMouseClick(e);
            base.OnMouseClick(e);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            anaPainter.OnMouseDown(e);
            base.OnMouseDown(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            anaPainter.OnMouseMove(e);
            base.OnMouseMove(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            anaPainter.OnMouseUp(e);
            base.OnMouseUp(e);
        }
    }
}
